Skip to content

Handle Self wrapped in Annotated when checking the expected self type - #21928

Merged
ilevkivskyi merged 2 commits into
python:masterfrom
EmmanuelNiyonshuti:fix-expected-annotated-self-type
Sep 8, 2026
Merged

Handle Self wrapped in Annotated when checking the expected self type#21928
ilevkivskyi merged 2 commits into
python:masterfrom
EmmanuelNiyonshuti:fix-expected-annotated-self-type

Conversation

@EmmanuelNiyonshuti

Copy link
Copy Markdown
Contributor

Fixes #21917

Self wrapped in Annotated was not being recognized as an expected self type. This PR updates is_expected_self_type to recurse through Annotated when checking unbound types.

@EmmanuelNiyonshuti EmmanuelNiyonshuti changed the title andle Self wrapped in Annotated when checking the expected self type Handle Self wrapped in Annotated when checking the expected self type Sep 2, 2026
@github-actions

This comment has been minimized.

@A5rocks A5rocks left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised we don't already simply trim out Annotated in all types! But this makes sense to me otherwise.

Comment thread test-data/unit/check-selftype.test
@github-actions

This comment has been minimized.

@EmmanuelNiyonshuti

Copy link
Copy Markdown
Contributor Author

Out of curiosity(not entirely sure if it's actually an issue), while snooping around to fix this issue, I noticed mypy is okay with this:

class Foo:
      def fails[T: Self](self: Annotated[T, "tag"], x: int) -> None: pass

I might be missing a lot of things since I'm fairly new to type checking semantics, but I think I couldn't find anything about this in PEP 673, and no other type checker allows this(I tried pyright, zuban, and ty). Is this intended on mypy's side?

@Moussa-Mikhail

Copy link
Copy Markdown

I've also found that the following has the same issue:

from typing import Self

type Test[T] = T

class Foo:
      # error: Method cannot have explicit self annotation and Self type  [misc]
      def also_fails(self: Test[Self], x: int) -> None: pass

Not a practical issue but it does seem odd.

@EmmanuelNiyonshuti
EmmanuelNiyonshuti force-pushed the fix-expected-annotated-self-type branch from 4749787 to f5696b4 Compare September 8, 2026 19:15
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi
ilevkivskyi merged commit 99eef8b into python:master Sep 8, 2026
25 checks passed
@EmmanuelNiyonshuti
EmmanuelNiyonshuti deleted the fix-expected-annotated-self-type branch September 9, 2026 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mypy errors on self: Annotated[Self, "tag"]

4 participants